<?php
/**
  * wechat php test
  */

//define your token
include("config.php");
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();

class wechatCallbackapiTest
{
	public function valid()
    {
        $echoStr = $_GET["echostr"];

        //valid signature , option
        if($this->checkSignature()){
        	echo $echoStr;
        	exit;
        }
    }



 //响应消息
     public function responseMsg()
     {
       $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
        if (!empty($postStr)){
             $this->logger("R \r\n".$postStr);
            $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
             $RX_TYPE = trim($postObj->MsgType);

           if (($postObj->MsgType == "event") && ($postObj->Event == "subscribe" || $postObj->Event == "unsubscribe")){
                 //过滤关注和取消关注事件
            }else{
                
           }             
             //消息类型分离
             switch ($RX_TYPE)
            {
                 case "event":
                    $result = $this->receiveEvent($postObj);
                    break;
                case "text":
                    if (strstr($postObj->Content, "第三方")){
                        $result = $this->relayPart3("http://www.fangbei.org/test.php".'?'.$_SERVER['QUERY_STRING'], $postStr);
                      }else{
                         $result = $this->receiveText($postObj);
                      }
                     break;
                 case "image":
                     $result = $this->receiveImage($postObj);
                     break;
                 case "location":
                     $result = $this->receiveLocation($postObj);
                     break;
                 case "voice":
                     $result = $this->receiveVoice($postObj);
                     break;
                 case "video":
                     $result = $this->receiveVideo($postObj);
                     break;
                 case "link":
                     $result = $this->receiveLink($postObj);
                     break;
                 default:
                     $result = "unknown msg type: ".$RX_TYPE;
                     break;
             }
             $this->logger("T \r\n".$result);
             echo $result;
       }else {
             echo "";
           exit;
         }
    }
 
   private function transmitNews($object, $newsArray)
    {
        if(!is_array($newsArray)){
            return;
        }
        $itemTpl = " 
				<item>
				<Title><![CDATA[%s]]></Title>
				<Description><![CDATA[%s]]></Description>
				<PicUrl><![CDATA[%s]]></PicUrl>
				<Url><![CDATA[%s]]></Url>
				</item>
                ";
        $item_str = "";
        foreach ($newsArray as $item){
            $item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);
        }
        $xmlTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>%s</ArticleCount>
<Articles>
$item_str</Articles>
</xml>";

        $result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray));
        return $result;
    }
  
  
  
  
  
  
  
  
  
  
  
  
}

?>